using System;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ScintillaNET;
using KRBTabControl;
using System.IO;

namespace FizzDevelop
{
	// This class is FizzDevelop's code editing component. It inherits from ScintillaNET's Scintilla object.
	class FizzCodeEditor : Scintilla
	{
		private Language language;	// The language the editor should be using for code folding and highlighting rules.
		private FizzFileTab tab; 	// The FizzTab that this page is a child of. 
		private bool showLineNumbers;
		private bool showFolding;
		private bool doLineWrapping;

		public FizzCodeEditor(FizzFileTab tab)
		{
			Dock = DockStyle.Fill;
			Margins.Margin0.Width = 15;
			Margins.Margin1.Width = 0;	// We don't need or want the marker margin by default.
			LineWrapping.Mode = LineWrappingMode.Word;
			LineWrapping.IndentSize = 2;
			Indentation.TabWidth = 4;
			Indentation.SmartIndentType = SmartIndent.Simple;
			BorderStyle = BorderStyle.None;
			Scrolling.HorizontalWidth = 20;
			ConfigurationManager.CustomLocation = "Styles.xml";
			Styles.LineNumber.BackColor = Color.DodgerBlue;
			Styles.LineNumber.ForeColor = Color.White;
			Margins.FoldMarginColor = Color.Transparent;
			Margins.FoldMarginHighlightColor = Color.Transparent;
			IsBraceMatching = true;
			ShowFolding = false;
			ShowLineNumbers = true;
			DoLineWrapping = true;
			tab.Controls.Add(this);	// Put this on the FizzTab
			tab.Editor = this;		// Set the FizzTab's Item to be this editor.
			this.tab = tab;			// Set the tab property.
			Language = Language.Plaintext;	// Now that the control has been added, we can set the Langauge. 
		}

		public bool DoLineWrapping
		{
			get { return doLineWrapping; }
			set
			{
				doLineWrapping = value;
				if (doLineWrapping)
					LineWrapping.Mode = LineWrappingMode.Word;
				else
					LineWrapping.Mode = LineWrappingMode.None;
			}
		}

		public bool ShowLineNumbers
		{
			get { return showLineNumbers; }
			set
			{
				showLineNumbers = value;
				if (showLineNumbers)
					Margins.Margin0.Width = Lines.Count.ToString().Length * 10 + (1 / Lines.Count.ToString().Length * 5);
				else
					Margins.Margin0.Width = 0;
			}
		}

		public bool ShowFolding
		{
			get { return showFolding; }
			set
			{
				showFolding = value;
				if (showFolding)
					Margins.Margin2.Width = 16;
				else
					Margins.Margin2.Width = 0;
			}
		}

		public FizzFileTab Tab	// This should not be editable outside the class, thus it only has a get accessor. 
		{
			get { return tab; }
		}

		public Language Language
		{
			get { return language; }
			set	// There is some code we need to run to change Scintilla's language. 
			{
				language = value;
				ConfigurationManager.Language = Languages.LanguageList[language].ScintillaKey;
				ConfigurationManager.Configure();
				if (language == Language.Plaintext) ShowFolding = false;
				else ShowFolding = true;
			}
		}
	}
}